JavaScript 56.7%
TypeScript 42.6%
1import { NextRequest, NextResponse } from "next/server";2import { prisma } from "@/lib/prisma";3import { saveVersion, saveFileFromBuffer } from "@/lib/storage";4import { logActivity } from "@/lib/activity";5import { MAX_VERSIONS_PER_FILE } from "@/lib/config";67export async function PUT(8 request: NextRequest,9 { params }: { params: { id: string } }10) {11 const file = await prisma.file.findUnique({ where: { id: params.id } });1213 if (!file) {14 return NextResponse.json({ error: "File not found" }, { status: 404 });15 }1617 const content = await request.text();1819 // Create version before overwriting20 const versionCount = await prisma.fileVersion.count({ where: { fileId: file.id } });21 const versionResult = await saveVersion(file.storagePath, file.name);2223 await prisma.fileVersion.create({24 data: {25 fileId: file.id,26 versionNum: versionCount + 1,27 storagePath: versionResult.storagePath,28 size: BigInt(versionResult.size),29 changeNote: "Auto-save",30 },31 });3233 // Cleanup old versions if over limit34 const versions = await prisma.fileVersion.findMany({35 where: { fileId: file.id },36 orderBy: { versionNum: "asc" },37 });38 if (versions.length > MAX_VERSIONS_PER_FILE) {39 const toDelete = versions.slice(0, versions.length - MAX_VERSIONS_PER_FILE);40 for (const v of toDelete) {41 const { deleteVersion } = await import("@/lib/storage");42 await deleteVersion(v.storagePath);43 await prisma.fileVersion.delete({ where: { id: v.id } });44 }45 }4647 // Write new content (chiffré au repos)48 const plain = Buffer.from(content, "utf-8");49 await saveFileFromBuffer(plain, file.storagePath);5051 await prisma.file.update({52 where: { id: params.id },53 data: { size: BigInt(plain.length), isEncrypted: true },54 });5556 await logActivity("EDIT", file.id, `Edited ${file.name}`);5758 return NextResponse.json({ success: true, size: plain.length });59}60